package com.xana.acg.mikomiko.frags.live;
import android.content.Context;
import android.view.View;
import android.widget.TextView;

import androidx.recyclerview.widget.GridLayoutManager;

import com.xana.acg.com.app.Fragment;
import com.xana.acg.com.data.DataSource;
import com.xana.acg.com.view.RoundImageView;
import com.xana.acg.com.view.recycler.RecyclerV;
import com.xana.acg.com.view.recycler.RecyclerAdapter;
import com.xana.acg.fac.model.biliLive.FaceItem;
import com.xana.acg.fac.net.NetCallBack;
import com.xana.acg.fac.net.Network;
import com.xana.acg.mikomiko.App;
import com.xana.acg.mikomiko.R;

import java.util.List;

import butterknife.BindView;


public class FaceFragment extends Fragment implements DataSource.Callback<List<FaceItem>>, RecyclerAdapter.AdapterListener<FaceItem> {
    @BindView(R.id.recyclerV)
    RecyclerV recycler;

    private Adapter mAdapter;

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_live_face;
    }


    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        listener = (OnClickListener) context;
    }

    @Override
    protected void initWidget(View root) {
        super.initWidget(root);
        recycler.setLayoutManager(new GridLayoutManager(getContext(), 2));
        recycler.setAdapter(mAdapter = new Adapter());
        mAdapter.setListener(this);
    }

    @Override
    protected void initData() {
        super.initData();
        Network.biliAPI().getFaces().enqueue(new NetCallBack(this));
    }

    @Override
    public void ok(List<FaceItem> resp) {
        mAdapter.add(resp);
    }

    @Override
    public void fail(String msg) {
        App.showToast(msg);
    }

    @Override
    public void onItemClick(RecyclerAdapter.ViewHolder holder, FaceItem faceItem) {
        if(listener!=null){
            listener.click(mAdapter.getItemList(), holder.getAdapterPosition());
        }
    }

    @Override
    public void onItemLongClick(RecyclerAdapter.ViewHolder holder, FaceItem faceItem) {

    }

    static class Adapter extends RecyclerAdapter<FaceItem> {

        @Override
        protected int getItemViewType(int position, FaceItem faceItem) {
            return R.layout.item_drawer_face;
        }

        @Override
        protected RecyclerAdapter.ViewHolder<FaceItem> onCreateViewHolder(View view, int viewType) {
            return new ViewHolder(view);
        }

        static class ViewHolder extends RecyclerAdapter.ViewHolder<FaceItem> {
            @BindView(R.id.coverImageView)
            RoundImageView cover;
            @BindView(R.id.nicknameTextView)
            TextView nickname;
            @BindView(R.id.titleTextView)
            TextView title;
            @BindView(R.id.areaTextView)
            TextView area;

            public ViewHolder(View itemView) {
                super(itemView);
            }

            @Override
            protected void onBind(FaceItem item) {
                cover.setSrc(item.getUser_cover());
                nickname.setText(item.getUname());
                title.setText(item.getTitle());
                area.setText(item.getAreaName());
            }
        }
    }

    private static OnClickListener listener;

    public interface OnClickListener{
        void click(List list, int index);
    }
}